package com.zxbit.flash.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.bean.AlertDialogData;
import com.zxbit.flash.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页弹框的（还款和逾期的）
 */
public class HomeTwoDialog extends Dialog {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_bill_type)
    TextView tvBillType;
    @BindView(R.id.tv_order_cash_num)
    TextView tvOrderCashNum;
    @BindView(R.id.tv_deadline)
    TextView tvDeadline;
    @BindView(R.id.tvBorrowTime)
    TextView tvBorrowTime;
    @BindView(R.id.relBorrowTime)
    RelativeLayout relBorrowTime;
    @BindView(R.id.tvRepaymentTime)
    TextView tvRepaymentTime;
    @BindView(R.id.relRepaymentTime)
    RelativeLayout relRepaymentTime;
    @BindView(R.id.tvOverdueTime)
    TextView tvOverdueTime;
    @BindView(R.id.relOverdueTime)
    RelativeLayout relOverdueTime;
    @BindView(R.id.tvOverdueMoney)
    TextView tvOverdueMoney;
    @BindView(R.id.relOverdueMoney)
    RelativeLayout relOverdueMoney;

    @BindView(R.id.relRepayment)
    LinearLayout relRepayment;
    @BindView(R.id.btConfirm)
    TextView btConfirm;
    @BindView(R.id.btRepayment)
    TextView btRepayment;
    @BindView(R.id.tvRepayment)
    TextView tvRepayment;
    @BindView(R.id.tvHint)
    TextView tvHint;

    //6还款 7逾期
    private int type;
    //展示数据
    private AlertDialogData alertDialogData;

    private Context mContext;
    private TwoClickListener clickListener;

    public HomeTwoDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_hometwo);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = mContext.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        if (alertDialogData != null) {

            tvOrderCashNum.setText(StringUtils.getDotMoneyStr(alertDialogData.getLoanAmt()));
            tvDeadline.setText(alertDialogData.getpTerm());

            tvBorrowTime.setText(alertDialogData.getOrderCrtTime());
            tvRepaymentTime.setText(alertDialogData.getRepayDate());

            if (type == 6) {
                relOverdueTime.setVisibility(View.GONE);
                relOverdueMoney.setVisibility(View.GONE);
                //6 还款
                tvTitle.setText(R.string.main_dialog_repay_title);
                tvOrderId.setText(alertDialogData.getOrderId());
                String typeText = mContext.getResources().getString(R.string.bill_type_lending);
                tvBillType.setTextColor(mContext.getResources().getColor(R.color.type_lending));
                tvBillType.setText(typeText);
                tvHint.setText(R.string.repayment_hint1);

                relRepayment.setVisibility(View.VISIBLE);
                tvRepayment.setVisibility(View.GONE);
                btConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (clickListener != null)
                            clickListener.onConfirm();
                    }
                });
                btRepayment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (clickListener != null)
                            clickListener.onRepayment();
                    }
                });

            } else {
                relRepayment.setVisibility(View.GONE);
                tvRepayment.setVisibility(View.VISIBLE);

                relOverdueTime.setVisibility(View.VISIBLE);
                relOverdueMoney.setVisibility(View.VISIBLE);
                //7 逾期
                tvTitle.setText(R.string.main_dialog_overdue_remind);
                tvOrderId.setText(alertDialogData.getOrderId());

                // 已逾期
                String typeText = mContext.getResources().getString(R.string.bill_type_overdue);
                tvBillType.setTextColor(mContext.getResources().getColor(R.color.type_overdue));
                tvBillType.setText(typeText);
                tvHint.setText(R.string.repayment_hint2);
                String dayUnit = mContext.getResources().getString(R.string.bill_days_unit);
                tvOverdueTime.setText(String.valueOf(alertDialogData.getOverdueDay()) + dayUnit);
                String moneyUnit = mContext.getResources().getString(R.string.money_unit_hint);
                tvOverdueMoney.setText(moneyUnit + StringUtils.getDotMoneyStr(alertDialogData.getOverduePrice()));

                tvRepayment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (clickListener != null)
                            clickListener.onRepayment();
                    }
                });
            }

        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public AlertDialogData getAlertDialogData() {
        return alertDialogData;
    }

    public void setAlertDialogData(AlertDialogData alertDialogData) {
        this.alertDialogData = alertDialogData;
    }

    public HomeTwoDialog setTwoClickListener(TwoClickListener listener) {
        clickListener = listener;
        return this;
    }

    public interface TwoClickListener {
        void onConfirm();

        void onRepayment();
    }
}
